package com.imooc.socketexample;

import java.io.*;
import java.net.Socket;

/**
 * Created by yijy on 2017/11/9.
 */
public class ServerThread extends Thread {

    private Socket socket;

    public ServerThread(Socket socket){
        this.socket = socket;
    }

    public void run(){
        InputStream is = null;
        BufferedReader br = null;
        OutputStream os = null;
        PrintWriter pw = null;
        try {
            is = socket.getInputStream();
            br = new BufferedReader(new InputStreamReader(is));
            String info="";
            while ((info=br.readLine())!=null){
                System.out.println(info);
            }
            socket.shutdownInput();
            os = socket.getOutputStream();
            pw = new PrintWriter(os);
            pw.print("welcome in server!");
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(os!=null)
                    os.close();
                if(br!=null)
                    br.close();
                if(is!=null)
                    is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
